Easy one? Access an object's property by using a variable in C# [closed]

Posted by ozke on Stack Overflow See other posts from Stack Overflow or by ozke
Published on 2009-05-26T10:23:45Z Indexed on 2010/06/17 10:53 UTC
Read the original article Hit count: 167

Filed under:
|

Possible Duplicate:
Property Name and need its value

Hi,

This one should be easy for most people. I'd like to access a normal variable in an object (let's say example) by using a variable value.

E.g.: I have a an array with a list of variable names and I want to get the information from the object by using them. Obviously this is wrong:

string[] variable_names = new string[] {"name", "surname"};
myObject.variable_names[0] = 1; 
myObject.variable_names[1] = 1;

In other languages (not C#) I use:

myObject[variable_names[0]] = 1;

I know the example looks stupid but it was just a way of explaining it.

Thanks in advance :)

© Stack Overflow or respective owner

Related posts about c#

Related posts about dynamic